/*
 * @Author: 何 海深 813936564@qq.com
 * @Date: 2022-08-23 21:08:54
 * @LastEditors: 何 海深 813936564@qq.com
 * @LastEditTime: 2024-01-27 19:28:41
 * @FilePath: \datashowapicture\src\store\modules\common.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from 'pinia'
import httpApi from '@/api/modules/userApi'
/* import riskApi from '@/api/modules/riskApi' */
export const commonStore = defineStore('common', {
  // 状态库
  state: () => ({
    userInfo: null, // 用户信息
    userEnterpriseList: null, //用户关联的企业
    fxModelInfo: [], //风险模型
    dicData: {} //字典
  }),
  getters: {},
  actions: {
    // tduck 根据token 获取用户信息
    async getUserInfo() {
      try {
        const res: any = await httpApi.getUserInfo().catch((err: any) => {
          if (err) {
            return false
          }
        })
        /* console.log("pinia获取用户信息", res);
        console.log(this); */
        if (res.code === 200) {
          this.userInfo = res.data
          return res
        } else {
          return false
        }
      } catch (error) {
        if (error) {
          return false
        }
      }
    },

    // 中台判断是否已经的领路
    async isAuthed() {
      try {
        const res: any = await httpApi.getIsAuthed({}).catch((err: any) => {
          if (err) {
            return false
          }
        })
        /* console.log("pinia获取用户信息", res); */
        if (res.success === true) {
          this.userInfo = res
          this.getFxModelInfo()
          this.userEnterprise()
          return res
        } else {
          return false
        }
      } catch (error) {
        if (error) {
          return false
        }
      }
    },
    // 获取用户关联的企业信息
    async userEnterprise() {
      /*  try {
        const res: any = await httpApi.getUserEnterprise().catch((err: any) => {
          if (err) {
            return false
          }
        })
        this.userEnterpriseList = [] as unknown as null
        if (res.success === true) {
          this.userEnterpriseList = res.body
          return res
        } else {
          return false
        }
      } catch (error) {
        if (error) {
          return false
        }
      } */
    },
    // 登录获取模型信息
    async getFxModelInfo() {
      /* try {
        const res: any = await riskApi.getFindFxModelInfo({ body: {} }).catch((err: any) => {
          if (err) {
            return false
          }
        })
        if (res.success === true) {
          this.fxModelInfo = res.body
          return res
        } else {
          return false
        }
      } catch (error) {
        if (error) {
          return false
        }
      } */
    },
    setDicData(key: string, list: Array<any> = []) {
      if (key) {
        this.dicData[key] = list
      }
    },
    async getDicData(key: string, val?: string) {
      if (this.dicData[key]) {
        return this.dicData[key]
      } else {
        if (!key) {
          return false
        }
        const resData = await httpApi.findDictData(key)
        const listData = resData.body?.map((item: any) => ({
          dictKey: item.dictKey,
          dictValue: item.dictValue
        }))
        this.setDicData(key, listData)
        return listData
      }
    }
  }
})
